﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Citi.Helpers
{
    public static class WebHelper
    {
        public static void DownloadRemoteFile(string url, string fileName, string networkUser, string networkPassword, string networkDomain, string proxy)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = new NetworkCredential(networkUser, networkPassword, networkDomain);

            try
            {
                request.Proxy = new WebProxy(proxy);
            }
            catch { }
            var response = (HttpWebResponse)request.GetResponse();

            // Check that the remote file was found. The ContentType
            // check is performed since a request for a non-existent
            // image file might be redirected to a 404-page, which would
            // yield the StatusCode "OK", even though the image was not
            // found.
            if ((response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Moved && response.StatusCode != HttpStatusCode.Redirect))
                return;

            // if the remote file was found, download oit
            using (var inputStream = response.GetResponseStream())
            {
                using (var outputStream = File.OpenWrite(fileName))
                {
                    var buffer = new byte[4096];
                    int bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);
                }
            }
        }

        //public static void DownloadRemoteFile(string uri, string fileName, string proxy)
        //{
        //    var request = (HttpWebRequest)WebRequest.Create(uri);
            
        //    try
        //    {
        //        request.Proxy = new WebProxy(proxy);
        //    }
        //    catch { }
        //    var response = (HttpWebResponse)request.GetResponse();

        //    // Check that the remote file was found. The ContentType
        //    // check is performed since a request for a non-existent
        //    // image file might be redirected to a 404-page, which would
        //    // yield the StatusCode "OK", even though the image was not
        //    // found.
        //    if ((response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Moved && response.StatusCode != HttpStatusCode.Redirect))
        //        return;

        //    // if the remote file was found, download oit
        //    using (var inputStream = response.GetResponseStream())
        //    {
        //        using (var outputStream = File.OpenWrite(fileName))
        //        {
        //            var buffer = new byte[4096];
        //            int bytesRead;
        //            do
        //            {
        //                bytesRead = inputStream.Read(buffer, 0, buffer.Length);
        //                outputStream.Write(buffer, 0, bytesRead);
        //            } while (bytesRead != 0);
        //        }
        //    }
        //}
    }
}
